<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 18:16
 */

namespace app\index\controller;


class Lines extends Common
{

    /*
     * 线路列表
     */
    public function query()
    {
        $type = empty(input('type')) ? 0 : input('type');
        #$order = empty(input('order')) ? '' : input('order');

        if(empty(input('order')) ){
            $type = 0;
        }else{
            $order = input('order');
        }
        if (empty(input('state'))){
            $where['grade_status'] = 2;
        }else{
            $where = array();
        }
        $al_db=db('address_line')->alias('al');
        $files = 'al.id,al.name,al.lines_id,al.createtime,al.updatetime';
        $db = $al_db->field($files)->where('linestatus',2);
        switch ($type){
            case 0:
                $res = $db->select();
                break;
            case 1:
                $res = $db->order('heat',$order)->select();
                break;
            case 2:
                $res = $db->order('updatetime',$order)->select();
                break;
            case 3:
                $res = $db->order('sitenum',$order)->select();
                break;
            case 4:
                $res = $db->order('score',$order)->select();
                break;
            default:
                $res = $db->select();
                break;
        }

        foreach ($res as $k=>$v){
            $v['lines_id'] = json_decode($v['lines_id'],true);
            $data = db('article')->field('grade_id,spotname')->where($where)->where('id','in',$v['lines_id'])->select();
            foreach ($data as $kk=>$vv){
                $icon = db('grade')->field('icon')->where('id', $vv['grade_id'])->find();
                $data[$kk]['icon'] = $icon;
            }
            $res[$k]['spotname'] = $data;

        }
        $page = input('page') > 0 ? input('page/d') : 1;
        $listRows = input('listRows') > 0 ? input('listRows/d') : 10;
        $total = count($res);
        $this->rest(['list' => $res, 'page' => getPage($page, $listRows, $total)]);
    }

    /*
     * 线路签到 【到此一游】
     * */
    public function circuit_sign()
    {
        if (empty(input('uid')) || empty(input('aid')) || empty(input('score')) || empty(input('state'))){
            $this->rest('参数错误','s004');
        }
        $db = db('graded');
        $data = [
            'uid' => input('uid'),
            'aid' => input('aid'),
            'score' => input('score'),
            'c_time' => time(),
            'modular' => 3,
            'state' => input('state'),
        ];

        $date = db('graded')->where('aid',$data['aid'])->where(['modular' => 3,'uid' => $data['uid']])->field('state')->find();
        if ($date['state'] == 2){
            $this->rest('该景点您已签到过了，欢迎再次光临','s004');
        }
        if ($data['state'] == 1 && $date['state'] == 1){
            db('graded')->where('state',1)->where('aid',$data['aid'])->where('uid',$data['uid'])->delete();
            $lists['look'] = db('graded')->where('state',1)->count();
            $lists['sign'] = db('graded')->where('state',2)->count();
            $this->rest($lists);
        }
        if (empty($date)){
            $list = $db->insert($data);
            if ($list){
                $sum['score'] = db('graded')->where(['modular' => 3,'state'=>2])->avg('score');
                $sum['look'] = db('graded')->where(['modular' => 3])->count();
                db('address_line')->where('id',$data['aid'])->update($sum);

                $lists['look'] = db('graded')->where(['modular' => 3,'state'=>1])->count();//想去人数
                $lists['sign'] = db('graded')->where(['modular' => 3,'state'=>2])->count();//到此一游人数
                $lists['message'] = '签到成功';
                $this->rest($lists);
            }else{
                $this->rest('签到失败，请稍后再试','s0004');
            }
        }

    }

    /*
     * 星旅指南----列表,景点,住宿,风味,名宿
     * */

    public function get_content()
    {
        $field = 'id,title,images,address,words,abstract,grade_id,uid,score,look';
        $data =[];
        $page = input('page') > 0 ? input('page/d') : 1;
        $listRow = input('listRows') > 0 ? input('listRows/d') : 10;
        for ($i=1;$i<=4;$i++){
            $res = db('Article')->where('grade_id','>',0)->where(['status'=>1,'categories_id'=>$i])->field($field)->page($page,$listRow)->select();
            foreach ($res as  $k=>$v){
                $icon = db('grade')->where('id',$v['grade_id'])->field('icon')->find();  # 评级图标
                $res[$k]['icon'] = $icon['icon'];
                $res[$k]['images'] = json_decode( $v['images'],true);
            }
            $data['type'.$i]['list'] = $res;
            $data['type'.$i]['page'.$i]['total'] = db('Article')->where('grade_id','>',0)->where(['status'=>1,'categories_id'=>$i])->count();
            $data['type'.$i]['page'.$i]['page'] = $page;
            $data['type'.$i]['page'.$i]['listRow'] = $listRow;
        }
        $this->rest($data);
    }

    /*
     *  获取线路详情
     * */

    public function get_lines_list()
    {
        $al = input('al');
        $uid = input('uid');
        if (empty($al) || empty($uid)) $this->rest('参数错误','s004');
        $aid_arr = db('address_line')->where('id',$al)->field('lines_id')->find();
        if (empty($aid_arr)) $this->rest('线路id有误','s004');
        $list = get_a_list(json_decode($aid_arr['lines_id']),false,1,1000,1);
        $num = db('address_line')->where('id',$al)->field('heat')->find();
        if (!empty($num)){
            @db('address_line')->where('id',$al)->update(['heat'=>$num['heat']+1]);
        }
        foreach ($list['list'] as $kk=>$v){
            $icon = db('grade')->field('icon')->where('id', $v['grade_id'])->find();
            $state = db('graded')->where(['modular'=>1,'uid'=>$uid,'aid'=>$v['id']])->field('state')->find();
            $list['list'][$kk]['state'] = $state['state'];
            $list['list'][$kk]['images'] = json_decode($v['images']);
            $islike = db('collection')->where(['uid'=>$uid,'pid'=>$al,'modular'=>'line','status'=>0,'type'=>'like'])->find();
            if(!empty($islike)){ # 是否点赞
                $list['list']['islike'] = 1;
            }else{
                $list['list']['islike'] = 0;
            }
            $isshare = db('collection')->where(['uid'=>$uid,'pid'=>$al,'modular'=>'line','status'=>0,'type'=>'share'])->find();
            if(!empty($isshare)){# 是否分享
                $list['list']['isshare'] = 1;
            }else{
                $list['list']['isshare'] = 0;
            }
            $iscollection = db('collection')->where(['uid'=>$uid,'pid'=>$al,'modular'=>'line','status'=>0,'type'=>'collection'])->find();
            if(!empty($iscollection)){# 是否收藏
                $list['list']['iscollection'] = 1;
            }else{
                $list['list']['iscollection'] = 0;
            }

            $data[$kk]['icon'] = $icon;
            $data[$kk]['title'] = $v['title'];
            unset($list['list'][$kk]['grade_id']);
        }
        $data['count'] = count($data);
        unset($list['total']);
        $list['data'] = $data;
        $this->rest($list);
    }


}